package net.xyuu.web;

import net.xyuu.data.Models;
import net.xyuu.entity.Category;
import net.xyuu.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by XYUU on 2016/11/27.
 */
@RestController
public class CategoryResource {

    @Autowired
    private CategoryRepository categoryRepository;

    //@PostMapping("/category")
    public Category post(@Valid @RequestBody Category category) {
        return categoryRepository.save(category);
    }

    @PostMapping("/category")
    public List<Category> post(@Valid @RequestBody Models<Category> models) {
        return categoryRepository.save(models.getModels());
    }

    @GetMapping("/category")
    public Page<Category> index(Pageable pageable) {
        return categoryRepository.findAll(pageable);
    }

    //@DeleteMapping("/category")
    public Category delete(@RequestBody Category category) {
        categoryRepository.delete(category);
        return category;
    }

    @DeleteMapping("/category")
    public List<Category> delete(@RequestBody Models<Category> models) {
        categoryRepository.delete(models.getModels());
        return models.getModels();
    }

}
